package br.com.lavitstudio.game;

import java.util.Random;

public class level_Caverns {
	private Block[][] block;
	
	public level_Caverns(Block[][] block) {
		this.block = block;
	}
	
	public void generateLevel() {
		for(int y=0;y<block.length;y++) {
			for(int x=0; x<block[0].length; x++) {
				if((y - 5) > (Level.worldH / 2)) {
					
					if((x + 1) < (Level.worldW - 10) && (x - 1) > 10 && (y + 1) < (Level.worldH - 10) && (y - 1) > 10) {
						
						if(block[x][y + 1].id == Tile.block.get("earth") && block[x][y - 1].id == Tile.block.get("earth")) {
						if(block[x][y + 2].id == Tile.block.get("earth") && block[x][y - 2].id == Tile.block.get("earth")) {
						if(block[x][y + 3].id == Tile.block.get("earth") && block[x][y - 3].id == Tile.block.get("earth")) {
						if(block[x][y + 4].id == Tile.block.get("earth") && block[x][y - 4].id == Tile.block.get("earth")) {
						if(block[x][y + 5].id == Tile.block.get("earth") && block[x][y - 5].id == Tile.block.get("earth")) {
	
						if(block[x + 1][y].id == Tile.block.get("earth") && block[x - 1][y].id == Tile.block.get("earth")) {
						if(block[x + 2][y].id == Tile.block.get("earth") && block[x - 2][y].id == Tile.block.get("earth")) {
						if(block[x + 3][y].id == Tile.block.get("earth") && block[x - 3][y].id == Tile.block.get("earth")) {
						if(block[x + 4][y].id == Tile.block.get("earth") && block[x - 4][y].id == Tile.block.get("earth")) {
						if(block[x + 5][y].id == Tile.block.get("earth") && block[x - 5][y].id == Tile.block.get("earth")) {
	
							if(new Random().nextInt(100) < 1) {
								block[x][y].id = Tile.block.get("cave");
								
								// Horizontal
								for(int left = 1; left < (new Random().nextInt(5) + 2); left++) {
									if((x - left) > 0 && block[x - left][y].id != Tile.block.get("solidair")) {
										block[x - left][y].id = Tile.block.get("cave");
									} else {
										break;
									}
								}
								
								for(int right = 1; right < (new Random().nextInt(5) + 2); right++) {
									if((x + right) < Level.worldW && block[x + right][y].id != Tile.block.get("solidair")) {
										block[x + right][y].id = Tile.block.get("cave");
									} else {
										break;
									}
								}
								
								// Vertical
								for(int top = 1; top < (new Random().nextInt(5) + 2); top++) {
									if((y + top) > 0 && block[x][y - top].id != Tile.block.get("solidair")) {
										block[x][y - top].id = Tile.block.get("cave");
									} else {
										break;
									}
								}
								
								for(int down = 1; down < (new Random().nextInt(5) + 2); down++) {
									if((y + down) < Level.worldH && block[x][y + down].id != Tile.block.get("solidair")) {
										block[x][y + down].id = Tile.block.get("cave");
									} else {
										break;
									}
								}
								
								// Diagonal (Left top)
								for(int left = 1; left < (new Random().nextInt(5) + 2); left++) {
									for(int top = 1; top < (new Random().nextInt(5) + 2); top++) {
										if((x - left) > 0 && block[x - left][y].id != Tile.block.get("solidair")) {
											if((y + top) > 0 && block[x][y - top].id != Tile.block.get("solidair")) {
												block[x - left][y - top].id = Tile.block.get("cave");
											} else {
												break;
											}
										} else {
											break;
										}
									}
								}
								
								// Diagonal (right top)
								for(int right = 1; right < (new Random().nextInt(5) + 2); right++) {
									for(int top = 1; top < (new Random().nextInt(5) + 2); top++) {
										if((x + right) < Level.worldW && block[x + right][y].id != Tile.block.get("solidair")) {
											if((y + top) > 0 && block[x][y - top].id != Tile.block.get("solidair")) {
												block[x + right][y - top].id = Tile.block.get("cave");
											} else {
												break;
											}
										} else {
											break;
										}
									}
								}
								
								// Diagonal (Left down)
								for(int left = 1; left < (new Random().nextInt(5) + 2); left++) {
									for(int down = 1; down < (new Random().nextInt(5) + 2); down++) {
										if((x - left) > 0 && block[x - left][y].id != Tile.block.get("solidair")) {
											if((y + down) < Level.worldH && block[x][y + down].id != Tile.block.get("solidair")) {
												block[x - left][y + down].id = Tile.block.get("cave");
											} else {
												break;
											}
										} else {
											break;
										}
									}
								}
								
								// Diagonal (right down)
								for(int right = 1; right < (new Random().nextInt(5) + 2); right++) {
									for(int down = 1; down < (new Random().nextInt(5) + 2); down++) {
										if((x + right) < Level.worldW && block[x + right][y].id != Tile.block.get("solidair")) {
											if((y + down) < Level.worldH && block[x][y + down].id != Tile.block.get("solidair")) {
												block[x + right][y + down].id = Tile.block.get("cave");
											} else {
												break;
											}
										} else {
											break;
										}
									}
								}
								
								
								
							}
						}
						}
						}
						}
						}
						}
						}
						}
						}
						}
					}
				}
			}
		}
	}
}
